var common = require("../../../../public.js");
Page({
  data: {
    userInfo: {},
    clock: '',
    day: '',
    hour: '',
    minute: '',
    second: '',
    itemId: '',
    time: '',
    storeId: '',
    shopData: {},
    Des: {},
    showParamPanle: false,
    isLoadingBusy: false,
    title: ''
  },
  onLoad: function (options) {
    const that = this
    let capsule = wx.getMenuButtonBoundingClientRect()
    this.setData({
        height: capsule.height,
        top: capsule.top,
    })
    var windowHeight = wx.getSystemInfoSync().windowHeight
    var windowWidth = wx.getSystemInfoSync().windowWidth
    this.setData({
        scroll_height: windowHeight * 750 / windowWidth - 200
    })
    if (options.name) {
      wx.setNavigationBarTitle({
        title: options.name //页面标题为路由参数
      })
    }
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data
        })
      }
    })
    this.getGoodsDes()
    wx.setNavigationBarTitle({
        title:'飞羊精选', //页面标题为路由参数
    })
  },
  getGoodsDes() {
    const that = this
    that.setData({
      isLoadingBusy: true
    })
    common.getJson(
      '/selectZerobuyById',
      {
        zerobuyId: that.options.id
      },
      function (res) {
        if (res.status === 200 && res.data) {
          const money = (res.data.goodsPrice / res.data.installmentCount).toFixed(2);
          that.setData({
            Des: res.data,
            title: res.data.goodsTitle,
            money: money,
            date: res.data.endTime,
            storeId: res.data.storeId,
          }, function () {
                                             
            // const total_micro_second = Math.floor((1701238740000 - new Date()) / 1000); 
            const total_micro_second = Math.floor((that.data.date - new Date()) / 1000); //计算时间差
            console.log('-----total_micro_second---',total_micro_second);
            if(total_micro_second>0){
                that.countDown(total_micro_second); //调用倒计时
            }else{
                this.setData({
                    day: 0,
                    hour: 0,
                    minute: 0,
                })
            }
         
            that.getShopDes()
            that.getDataDes(res.data.goodsId);
          })
        } else {
          common.showRequestErrMsg(res)
        }
      },
      function () {
        that.setData({
          isLoadingBusy: false
        })
      }
    )
  },
  //获取店铺信息
  getShopDes: function () {
    var that = this
    common.getJson(
      '/store/info',
      {
        storeId: this.data.storeId
      },
      function (res) {
        if (res.status === 200) {
          that.setData({
            shopData: res.data,
            mobile: res.data.mobile,
          })
        } else {
          common.showRequestErrMsg(res)
        }
      }
    )
  },
  callme: function (times) {
    wx.makePhoneCall({
      phoneNumber: this.data.mobile,
    })
  },
  clickParam() {
    this.setData({
      showParamPanle: true
    })
  },
  countDown: function (times) {
    var that = this;
    var timer = null;
    timer = setInterval(function () {
      var day = 0,
        hour = 0,
        minute = 0,
        second = 0; //时间默认值
      if (times > 0) {
        day = Math.floor(times / (60 * 60 * 24));
        hour = Math.floor(times / (60 * 60)) - (day * 24);
        minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);
        second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
      }
      if (day <= 9) day = '0' + day;
      if (hour <= 9) hour = '0' + hour;
      if (minute <= 9) minute = '0' + minute;
      if (second <= 9) second = '0' + second;
      //
      that.setData({
        day: day,
        hour: hour,
        minute: minute,
        second: second,
        time: day + ":" + hour + ":" + minute + ":" + second
      })
      times--;
    }, 1000);
    if (times <= 0) {
      clearInterval(timer);
    }
  },
  toShop() {
    console.log(this.data.storeId)
    wx.navigateTo({
      url: '/packageA/pages/Goods/storePage/index?id=' + this.data.storeId
    })
  },
  collection() {
    common.getJson(
      '/store/favo/add',
      {
        storeId: this.data.storeId,
        token: this.data.userInfo.token,
        userId: this.data.userInfo.user.id,
      },
      function (res) {
        if (res.msg == 'OK') {
          common.showToast('店铺收藏成功', 'success')
        }
      }
    )
  },
  goBack() {
    wx.navigateBack({
        url: '../../index/index'
    })
},
  onClose() {
    this.setData({
      showParamPanle: false
    })
  },

    //查询商品详情信息
    getDataDes(idx){
        var that = this
        var tempJson = {
            itemId: idx

        }
        common.post("/item/info", tempJson, function (res) {
            if (res.status === 200) {
                //itemAttrList没有数据隐藏商品属性模块 有数据显示遍历
                if (res.data.itemAttrList.length == 0) {
                    that.setData({
                        property: true
                    })
                } else {
                    that.setData({
                        property: false,
                        itemAttrList: res.data.itemAttrList,
                        image: res.data.image,
                        title: res.data.title
                    })
                }
                // var newResData = JSON.parse( res.data.itemDesc);
                const richText = res.data.itemDesc ? res.data.itemDesc.replace(/\<img/gi, "<img mode='aspectFill' ") : ''

                let str = richText.replace(/<img[^>]*>/gi, function (match, capture) {
                    return match.replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/ig, 'style="max-width:100%;height:auto; width:100%";height:100%') // 替换style
                    //  match.replace(/style\s*?=\s*?([‘"])[\s\S]*?\1/ig, 'style="max-width:100%;height:auto; width:100%";height:100%') // 替换style      
                })
                console.log(str,"ssssssssssssssssssssssss")
                that.setData({
                    // Des: res.data,
                    title: res.data.title,
                    storeId: res.data.storeId,
                    goodsInformation: str,
                    sellPoint: res.data.sellPoint
                }, function () {
                    // that.getShopDes(); //店铺信息
                    // that.getPostMethod(); // 获取配送方式
                    // // that.getshopArrtention(); //是否关注店铺
                })
            } else {
                common.showRequestErrMsg(res)
            }
        }, function () {
            that.setData({
                isLoadingBusy: false
            })
        })
    },	 
});

// var total_micro_second = 1546235640000 - (new Date());
// /* 毫秒级倒计时 */
// function count _down(that) {
//   // 渲染倒计时时钟
//   that.setData({
//     clock: date_format(total_micro_second)
//   });

//   if (total_micro_second <= 0) {
//     that.setData({
//       clock: "已经截止"
//     });
//     // timeout则跳出递归
//     return;
//   }
//   setTimeout(function () {
//     // 放在最后--
//     total_micro_second -= 10;
//     count_down(that);
//   }
//     , 10)
// }
// // 时间格式化输出,每10ms都会调用一次
// function date_format(micro_second) {
//   var day = parseInt(micro_second / 1000 / 60 / 60 / 24, 10); //计算剩余的天数 
//   var hr = parseInt(micro_second / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 
//   var min = parseInt(micro_second / 1000 / 60 % 60, 10); //计算剩余的分钟 
//   var sec = parseInt(micro_second / 1000 % 60, 10); //计算剩余的秒数 
//   day = checkTime(day);
//   hr = checkTime(hr);
//   min = checkTime(min);
//   sec = checkTime(sec);
//   return day + ":" + hr + ":" + min + ":" + sec;
// }
// function checkTime(i) {
//   if (i < 10) {
//     i = "0" + i;
//   }
//   return i;
// }
// // 位数不足补零
// function fill_zero_prefix(num) {
//   return num < 10 ? "0" + num : num
// }